package com.example.minesweepers;

import high_scores.HighScores;
import high_scores.Player;
import interfaces.IPlayer;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import com.google.gson.Gson;

public class GameActivity extends Activity {
	private static final String MINESWEEPER_MASTER = "MinesweeperMaster";
	private static final String MINESWEEPER_LARGE = "MinesweeperLarge";
	private static final String MINESWEEPER_MEDIUM = "MinesweeperMedium";
	private static final String MINESWEEPER_SMALL = "MinesweeperSmall";
	private int size;
	private String name;
	private HighScores highScores;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_game);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		startActivity();
	}

	private void startActivity() {
		highScores = new HighScores();
		loadHighScores();
		
		Intent intent = getIntent();
		size = intent.getIntExtra("size", 0);
		name = intent.getStringExtra("name");
		
		MinesweeperController controller = new MinesweeperController(this, size, size);
		controller.startNewGame();
	}
	
	private void loadHighScores() {
		SharedPreferences settings = getSharedPreferences(filename(size), MODE_PRIVATE);
		Gson gson = new Gson();
		
		int sizeToLoad = settings.getInt("Size", 0);
		for(int i = 0; i < sizeToLoad; i++) {
			String json = settings.getString("Player" + i, "");
			IPlayer player = gson.fromJson(json, Player.class);
	    	highScores.putUnsorted(player);
		}
	}

	public boolean saveGame(String time) {
		if(time != null && name != null && !time.equals("") && !name.equals("")) {
			addGame(time);
			Log.i("game", "added" + highScores.size());
			return true;
		}
		return false;
	}

	private void addGame(String time) {
		IPlayer player = new Player(name, time);
		highScores.putSorted(player);
	}
	
	private String filename(int size) {
		if(size < 10) {
			return MINESWEEPER_SMALL;
		}
		else if(size < 15) {
			return MINESWEEPER_MEDIUM;
		}
		else if(size < 20) {
			return MINESWEEPER_LARGE;
		}
		else {
			return MINESWEEPER_MASTER;//default
		}
	}
	
	@Override
	protected void onDestroy() {
		saveHighScores();
		finish();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		onDestroy();
		super.onPause();
	}

	private void saveHighScores() {
		SharedPreferences settings = getSharedPreferences(filename(size), MODE_PRIVATE);
		Editor editor = settings.edit();
	    Gson gson = new Gson();
	    editor.putInt("Size", highScores.size());
	    
	    for(int i = 0; i < highScores.size(); i++) {
	    	String json = gson.toJson(highScores.getPlayer(i));
		    editor.putString("Player" + i, json);
	    }
	   
		editor.commit();
	}
	
	@Override
	protected void onResume() {
		startActivity();
		super.onResume();
	}
}